﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;
using System.Text;
using System.Threading.Tasks;

namespace SimdTest2.Methods
{
    [Algorithm(true, AlgorithmType.Five)]
    public unsafe class FiveMethod : AlgorithmBase
    {
        public FiveMethod(int row, int col) : base(row, col)
        {
        }
        int[] _data;
        Random rng = new Random();
        protected override void CreateData()
        {
            _data = new int[_size];
            for (int i = 0; i < _data.Length; i++)
            {
                _data[i] = i;
            }
        }
        protected override void OrdinaryMethod()
        {
            
            int n = (int)_size;
            while (n-- > 1)
            {
                int k = rng.Next(n + 1);
                int value = _data[k];
                _data[k] = _data[n];
                _data[n] = value;
            }
            //Random.Shared.Shuffle(_data);
        }
        readonly int[] mask = { 30, 39, 45, 54, 57, 75, 78, 99, 108, 114, 120, 135, 141, 147, 156, 177, 180, 198, 201, 210, 216, 225, 228 };
        protected override void SimdMethod()
        {
            SimdMethod1();
        }
        protected void SimdMethod1()
        {
            int n = (int)_size;
            int vsize = Vector128<int>.Count;
            int m = n - (n & (vsize - 1));
            fixed (int* ptr = _data, maskPtr = mask)
            {
                int j = 0;
                int i = 0;
                for (j = 0; j < m; j += vsize)
                {
                    Vector128<int> v = *(Vector128<int>*)(ptr + j);
                    // 打乱
                    var t = Sse2.Shuffle(v, (byte)*(maskPtr + rng.Next(mask.Length)));
                    // 拷贝
                    Sse2.Store(ptr + j, t);
                    j += vsize;
                }
                for (; j < n; j++)
                {
                    int* t = ptr + rng.Next(n);
                    int* val = ptr + j;
                    *val = (*t ^= *val ^= *t) ^ *val;
                }
            }
        }
        protected void SimdMethod2()
        {
            int n = (int)_size;
            int vsize = Vector128<int>.Count;
            int m = n - (n & (vsize - 1));
            Vector128<int>[] vectors = new Vector128<int>[m / vsize];
            fixed (int* ptr = _data, maskPtr = mask)
            {
                fixed (Vector128<int>* vptr = vectors)
                {
                    int j = 0;
                    int i = 0;
                    for (j = 0; j < m; j += vsize)
                    {
                        *(vptr + i++) = *(Vector128<int>*)(ptr + j);
                    }
                    j = 0;
                    for (i = 0; i < vectors.Length; i++)
                    {
                        Sse2.Store(ptr + j, Sse2.Shuffle(*(vptr + i), (byte)*(maskPtr + rng.Next(mask.Length))));
                        j += vsize;
                    }
                    for (; j < n; j++)
                    {
                        int* t = ptr + rng.Next(n);
                        int* val = ptr + j;
                        int temp = *t;
                        *t = *val;
                        *val = temp;
                    }
                }
            }
        }
    }
}
